package com.mark.web.sys.controller.frontend;

import com.mark.common.CurrentUser;
import com.mark.common.ServerResponse;
import com.mark.web.sys.common.enmus.UserStateEnum;
import com.mark.web.sys.entity.from.UserUpdateFrom;
import com.mark.web.sys.entity.po.User;
import com.mark.web.sys.entity.vo.UserVo;
import com.mark.web.sys.service.IUserImgService;
import com.mark.web.sys.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @Description:
 * @Author: Kingsley
 * @CreateDate: 2019/1/1 15:25
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Api(tags = "系统-用户模块")
@RestController(value = "sysUserController")
@RequestMapping("/sys/user/")
public class UserController {

    private final IUserService iUserService;

    @Autowired
    public UserController(IUserService iUserService) {
        this.iUserService = iUserService;
    }

    /**
     * 5.获取当期用户的登录信息
     *
     * @return ：
     */
    @PostMapping(value = "check_user_info.do")
    public ServerResponse<UserVo> checkLogin(){
        int currentUserId = CurrentUser.getCurrentUserId();
        return iUserService.getCurrentUser(currentUserId);
    }

    /**
     * 9.登录状态下更新密码
     *
     * @param passwordNew：旧密码
     * @param passwordOld：新密码
     * @return ： 更新密码的结果
     */
    @PostMapping(value = "reset_password.do")
    public ServerResponse<String> resetPassword(HttpServletRequest httpServletRequest, String passwordNew, String passwordOld) {
        if (StringUtils.isBlank(passwordNew) || StringUtils.isBlank(passwordOld)) {
            return ServerResponse.createByIllegalArgument("输入的密码不能为空");
        }
        int currentUserId = CurrentUser.getCurrentUserId();
        return iUserService.resetPassword(passwordOld, passwordNew, currentUserId);
    }

    /**
     * 用户更新信息
     *
     * @param userUpdateFrom：用户信息校验表单
     * @param bindingResult：校验的结果
     * @return ：
     */
    @ApiOperation(value = "用户更新信息")
    @PostMapping(value = "update_information.do")
    public ServerResponse<UserVo> updateInformation(@Valid UserUpdateFrom userUpdateFrom, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ServerResponse.createByErrorMessage(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        User updateInfo = new User();
        updateInfo.setId(CurrentUser.getCurrentUserId());
        BeanUtils.copyProperties(userUpdateFrom, updateInfo);

        //用户性别的校验
        if(StringUtils.isNotBlank(userUpdateFrom.getGender())){
            List<String> userGenderTypeList = UserStateEnum.getUserGenderTypes();
            if(userGenderTypeList.contains(userUpdateFrom.getGender())){
                UserStateEnum userGenderEnum = UserStateEnum.stateInfoOf(userUpdateFrom.getGender());
                if(userGenderEnum == null){
                    return ServerResponse.createByIllegalArgument("输入的性别值不合法！");
                }else{
                    int gender = userGenderEnum.getState();
                    updateInfo.setGender((short) gender);
                }
            }else{
                return ServerResponse.createByIllegalArgument("输入的性别值不合法！");
            }
        }
        ServerResponse<UserVo> serverResponse = iUserService.updateInformation(updateInfo);
        // TODO: 2019/1/12
        //更新Redis中的用户信息
        return serverResponse;
    }

    /**
     * 11.获取用户信息
     *
     * @return : 当前用户信息
     */
    @PostMapping(value = "get_information.do")
    public ServerResponse<UserVo> getInformation() {
        Integer currentUserId = CurrentUser.getCurrentUserId();
        return iUserService.getInfo(currentUserId);
    }

    /**
     *13.更新用户头像
     */
    @ApiOperation(value = "更新用户头像")
    @PostMapping(value = "update_image.do")
    @ApiImplicitParam(name = "imageUrl",value = "头像的url地址")
    public ServerResponse updateUserImageUrl(HttpServletRequest httpServletRequest, String imageUrl){
        int currentUserId = CurrentUser.getCurrentUserId();
        return iUserService.updateImageUrl(currentUserId,imageUrl);
    }
}
